//
//  STMsgNumView.m
//  SimulationStock
//
//  Created by Allen1 on 14-2-14.
//  Copyright (c) 2014年 zhouyu. All rights reserved.
//

#import "STMsgNumView.h"

#define kKey1 @"atten_info_num"
#define kKey2 @"my_info_num"

@interface STMsgNumView ()
@property (nonatomic, strong) UIImageView *iv;
@property (nonatomic, strong) UILabel *labelNum;

@end

@implementation STMsgNumView
- (void)dealloc
{
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = NO;
        self.iv = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-21, (frame.size.height-21)/2.0f, 21, 21)];
        UIImage *image = [UIImage imageNamed:@"bg_number.png"];
        [self.iv setImage:[image stretchableImageWithLeftCapWidth:11 topCapHeight:11]];
        [self addSubview:self.iv];
        
        self.labelNum = [[UILabel alloc] initWithFrame:self.iv.bounds];
        self.labelNum.textAlignment = ZYTextAlignmentCenter;
        self.labelNum.text = @"0";
        self.labelNum.backgroundColor = [UIColor clearColor];
        self.labelNum.textColor = [UIColor whiteColor];
        self.labelNum.font = [UIFont systemFontOfSize:11];
        self.labelNum.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.iv addSubview:self.labelNum];
        
        [self setFrameForIv];
        
        self.hidden = YES;

    }
    return self;
}

- (void)setFrameForIv
{
    NSString *str = self.labelNum.text;
    CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:10] constrainedToSize:CGSizeMake(50, 21) lineBreakMode:NSLineBreakByCharWrapping];
  
    
    size.width += 7;
    if (size.width < 21) {
        size.width = 21;
    }
    [self.iv setFrame:CGRectMake(self.frame.size.width-ceilf(size.width), (self.frame.size.height-21)/2, ceilf(size.width), 21)];
}

- (void)makeNumWithStr:(NSString *)str {
    if (ZYIsNullOrEmpty(str)) {
        self.labelNum.text = @"";
        [self setFrameForIv];
        self.hidden = NO;
        return;
    }
    int count = str.intValue;
    
    NSString *strCount = @"0";
    if (count > 99) {
        strCount = @"...";
        self.labelNum.text = strCount;
        [self setFrameForIv];
        self.hidden = NO;
        
    }else if (count > 0){
        strCount = str;
        self.labelNum.text = strCount;
        [self setFrameForIv];
        self.hidden = NO;
    }
    else
    {
        self.hidden = YES;
    }
}
@end
